<template>
    <div id="switchover">
      <div id="form">
        <span>
          <h2 style="color: white;margin-top: 50px">云南职疫情大数据监控平台</h2>
        </span>
        <br>
        <br>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="110px" class="demo-ruleForm">
          <el-form-item label="UserName:" prop="name" class="liable">
            <el-input v-model="ruleForm.name"  placeholder="请输入用户名" maxlength="16" show-word-limit id="username" prefix-icon="el-icon-user" style="width: 280px;"></el-input>
          </el-form-item>
          <el-form-item label="PassWord:" prop="pass" class="liable">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"  placeholder="请输入密码" maxlength="16" id="userpassword" prefix-icon="el-icon-lock"  style="width: 280px;"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')" id="login">登 录</el-button>
            <el-button @click="resetForm('ruleForm')" id="reset">重 置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'Login',
  data () {
    return {
      ruleForm: {
        name: '',
        pass: '',
      },
      rules: {
        name: [{ required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' },
          { pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+$/, message: '不允许输入空格等特殊符号' }
        ],
        pass: [
          { required: true,message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
          { pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+$/, message: '不允许输入空格等特殊符号' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      let rs;
      // 获取用户输入的用户名和密码
      var userName = document.getElementById("username").value
      var userPassword = document.getElementById("userpassword").value
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 发送 POST 请求
          await axios({
            method: 'post',
            url: 'http://114.132.150.39:8080/login',
            data: {
              name: userName,
              password: userPassword
            }
          }).then(function (response) { // 请求成功
            rs = response.data.token
            console.log(rs)
          })
          if (rs != null) {
           window.sessionStorage.setItem('token',rs);
            this.$message({
              showClose: true,
              message: '欢迎回来，我们恭候你多时了！',
              type: 'success'
            })
            this.$router.push("/ynnz")
          } else {
            this.$message({
              showClose: true,
              message: '用户名或密码错误，请重新输入！',
              type: 'error'
            })
          }
        } else {
          this.$message({
            showClose: true,
            message: '账号或密码为空，请重新输入！',
            type: 'error'
          })
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style>
#switchover{
  width: 100%;
  height: 100%;
  background: url("../../assets/pageBg.png") no-repeat;
  background-size: 100% 100%;
  background-attachment: fixed;
}
#form{
  width: 420px;
  height: 300px;
  text-align: center;
  position: absolute;
  top: 18%;
  left: 33%;
  border: 1px white solid;
  border-radius: 25px;
}
.liable .el-form-item__label{
  color: white;
  font-size: 18px;
  margin-left: 5px;;
}
#login{
  float: left;
}
</style>
